/**
 * 获取文件类型
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        printf("Usage: %s file1 file2 ....", argv[0]);
    }

    int i = 1;
    struct stat stat_buf;
    for (; i < argc; ++i)
    {
        memset(&stat_buf, 0, sizeof(stat_buf));
        if(lstat(argv[i],&stat_buf) < 0){
            fprintf(stderr,"lstat error %s\n",strerror(errno));
            exit(EXIT_FAILURE);
        }

        printf("%s\t",argv[i]);

        if(S_ISREG(stat_buf.st_mode)){
            printf("Regular file");
        }
        else if(S_ISDIR(stat_buf.st_mode)){
            printf("Directory file");
        }
        else if(S_ISCHR(stat_buf.st_mode)){
            printf("Character Device");
        }
        else if(S_ISBLK(stat_buf.st_mode))
        {
            printf("Block Device");
        }
        else if(S_ISFIFO(stat_buf.st_mode)){
            printf("FIFO file");
        }
        else if(S_ISSOCK(stat_buf.st_mode)){
            printf("Socket file");
        }
        else if(S_ISLNK(stat_buf.st_mode)){
            printf("Symbolic link file");
        }
        printf("\n");
    }

    return 0;
}